<?php
// config items
$config = new RM_Config();
$currencySymbol = $config->getValue('rm_config_currency_symbol');
$criteria = RM_Reservation_Manager::getInstance()->getCriteria();
$message = false;
$calculatedTotalPrice = 0;
$otherInfoSelection = "";

// get the price if the criteria is set...
if (isset($criteria) && ($criteria->start_datetime && $criteria->end_datetime)){

    if ($criteria !== null) {
        if ($criteria->start_datetime && $criteria->end_datetime) {
            $period = new RM_Reservation_Period(
                new RM_Date(strtotime($criteria->start_datetime)),
                new RM_Date(strtotime($criteria->end_datetime))
            );
        }

        $persons = new RM_Reservation_Persons(array("adults"=>$criteria->adults,"children"=>$criteria->children,"infants"=>$criteria->infants));

        $information = new RM_Prices_Information($this->unit, $period, $persons, $criteria->otherinfo);
        $priceSystem = RM_Environment::getInstance()->getPriceSystem();


        $otherInfoSelection = $criteria->otherinfo['value'];
     }

    try {
        $calculatedTotalPrice = $priceSystem->getTotalUnitPrice($information);
    } catch (Exception $e) {
        $calculatedTotalPrice = 0;
        $message = $e->getMessage();
    }

    if ((bool)$config->getValue('rm_config_prices_with_tax')) {
        $calculatedTotalPrice += RM_Environment::getInstance()->getTaxSystem()->calculateTotalTax($this->unit, $calculatedTotalPrice);
    }

    /**
     * this checks if the unit has any partial reservations
     * This is only used when the flexible criteria option is selected.
     */
    if ($criteria->flexible && $message===false){
        $unitDAO = new RM_Units();
        $reservations = $unitDAO->isFlexibleAvailable($this->unit,$period);

        if (!empty($reservations)){
            $message = $this->_translate->_('User.Unit.Details','PartiallyAvailable');
        }
    }

}
//$dateFormat = $config->getJSDateformat();
$dateFormat = RM_Prices_Manager::getInstance()->getRealPriceSystem($this->unit)->getDateformat();
// panel config
$panelsettings = Zend_Json::decode($this->panel->settings);
$align = $panelsettings['align'];
?>
<script type="text/javascript">

function RM_Hospitality_DoShadowBox(datepickerURL, datepickerWidth, datepickerHeight){
    RM_doShadowBox(
        datepickerURL,
        "self",
        datepickerWidth,
        datepickerHeight,
        "iframe"
    );
}

var personsPickerURL = RM.Common.AssembleURL({
        controller : 'Hospitality',
        action: 'personspicker',
        parameters: [{
            name: "unit_id",
            value: "<?php echo $this->unit->id;?>"
        }]
});

var accomPickerURL = RM.Common.AssembleURL({
        controller : 'Hospitality',
        action: 'accompicker',
        parameters: [{
            name: "unit_id",
            value: "<?php echo $this->unit->id;?>"
        }]
});

function detailsNotComplete(details){

        document.getElementById("RM_unit_details_booknowbutton_button").onclick = function(){
            alert("hi");
        }

}

</script>

<div id="<?php echo $this->panel->getDivID() ?>">
    <div class="RM_unit_details_selectioninfo_container" style="text-align:<?php echo $panelsettings['align'];?>">
    <fieldset><legend><?php echo $this->_translate->_('User.Unit.Details','SelectionInfo');?></legend>
        <table width="100%" border="0" cellspacing="0" cellpadding="0">
             <tr>
                <td>
                    <?php echo $this->_translate->_('User.Unit.Details','SelectedStartDate');?>
                    &nbsp;</td>
                <td>
                    <b>
                    <?php
                    if (isset($criteria) && $criteria->start_datetime !== ""){
                        echo date($dateFormat, strtotime($criteria->start_datetime));
                    } else {
                        echo $this->_translate->_('User.Unit.Details','NoDatesSelected');
                    } 
                    ?>
                    </b>
                </td>
                <td rowspan="2">

                    <div align="left"><a href="javascript:void(0)" onClick="rm_unit_details_selectDates(datepickerURL,datepickerWidth,datepickerHeight)">
                        <img src="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/userdata/plugins/Hospitality/images/dates-edit.png" border="0" alt="<?php echo $this->_translate->_('User.Hospitality.Main','Edit');?>" /><?php echo $this->_translate->_('User.Hospitality.Main','Edit');?></a> </div></td>
             </tr>
             <tr>
                <td>
                    <?php echo $this->_translate->_('User.Unit.Details','SelectedEndDate');?>
                    &nbsp;</td>
                <td>
                    <b>
                    <?php
                    if (isset($criteria) && $criteria->end_datetime !=="" ){
                        echo date($dateFormat, strtotime($criteria->end_datetime));
                    } else {
                        echo $this->_translate->_('User.Unit.Details','NoDatesSelected');
                    }
                    ?>
                    </b>
                </td>
             </tr>             
              <tr>
                <td colspan="3"><hr></td>
              </tr>
              <tr>
                <td valign="top"><?php echo $this->_translate->_('User.Hospitality.Main','SelectedAccom');?></td>
                <td valign="top">
                    <b>
                    <?php
                    if (isset($criteria) && $criteria->otherinfo !== null){
                        echo $this->_translate->_('User.Hospitality.Main', $criteria->otherinfo['value']);
                    } else {
                        echo $this->_translate->_('User.Hospitality.Main','NotSelected');
                        $message = $this->_translate->_('User.Hospitality.Main','MissingInfo');
                    }
                    ?>
                    </b>
                </td>
                <td valign="top">
                    <?php
                    if (isset($criteria) && $criteria->otherinfo !== null){
                    ?>
                    <a href="javascript:void(0)" onClick="RM_Hospitality_DoShadowBox(accomPickerURL,400,270)">
                        <img src="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/userdata/plugins/Hospitality/images/accom-edit.png" border="0" alt="<?php echo $this->_translate->_('User.Hospitality.Main','Edit');?>" /><?php echo $this->_translate->_('User.Hospitality.Main','Edit');?>
                    </a>
                    <?php
                    }
                    ?>
                </td>
              </tr>
              <tr>
                <td valign="top"><?php echo $this->_translate->_('User.Hospitality.Main','SelectedPeople');?></td>
                <td valign="top">
                <b>
                <?php
                if (isset($criteria) && $criteria->adults !== null){
                    echo $this->_translate->_('User.Hospitality.Main','Adults').": ".$criteria->adults."<br/>";
                    if (isset($criteria) && (int)$criteria->children !== 0) echo $this->_translate->_('User.Hospitality.Main','Children').": ".$criteria->children."<br/>";
                    if (isset($criteria) && (int)$criteria->children !== 0) echo $this->_translate->_('User.Hospitality.Main','Infants').": ".$criteria->infants;
                } else {
                    echo $this->_translate->_('User.Hospitality.Main','NotSelected');
                    $message = $this->_translate->_('User.Hospitality.Main','MissingInfo');
                }
                ?>
                </b>
                </td>
                <td valign="top">
                    <?php
                    if (isset($criteria) && $criteria->start_datetime !== null){
                    ?>
                    <a href="javascript:void(0)" onClick="RM_Hospitality_DoShadowBox(personsPickerURL,265,250)">
                        <img src="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/userdata/plugins/Hospitality/images/persons-edit.png" border="0" alt="<?php echo $this->_translate->_('User.Hospitality.Main','Edit');?>" /><?php echo $this->_translate->_('User.Hospitality.Main','Edit');?>
                    </a>
                    <?php
                    }
                    ?>
                </td>
              </tr>
              <tr>
                <td colspan="3"><hr></td>
              </tr>
             <?php
             if (!isset($criteria) || ($criteria->end_datetime === null || $criteria->start_datetime === null)){
             ?>
             <tr>
                <td colspan="3">
                    <div class="RM_unit_details_selectioninfo_warning">
                            <?php echo $this->_translate->_('User.Unit.Details','NoDatesSelected');?>
                    </div>                </td>
             </tr>
             <?php
             }
             if ($calculatedTotalPrice>0){
             ?>
             <tr>
                <td>
                    <?php echo $this->_translate->_('User.Unit.Details','SelectionTotal');?>
                </td>
                <td colspan="2">
                    <b><?php echo $currencySymbol; echo $calculatedTotalPrice;?></b>
                </td>
             </tr>
             <?php
             } else {
             ?>
             <tr>
                <td colspan="3">
                    <div class="RM_unit_details_selectioninfo_warning">
                        <?php echo $this->_translate->_('User.Unit.Details','TotalNotAvailable');?>
                    </div>
                </td>
             </tr>
             <?php
             }
             if (isset($message)){
             ?>
             <tr>
                <td colspan="3">
                    <div class="RM_unit_details_selectioninfo_warning">
                        <?php echo $message;?>
                    </div>
                </td>
             <?php
             }
             ?>
            </tr>
        </table>
    </fieldset>
    </div>
</div>
<input id="RM_Hospitality_otherInfo" type="hidden" name="otherInfo[board_type]" value="<?php echo $otherInfoSelection;?>">
